from util import my_tkinter, dp_helpe_V_file, dp_helper, cv2_util, file_helper_V_file
import pandas as pd
from pojo.Product import *
from service import pc_爬虫_service
from service.cpss_产品搜索_service import *
import json
import winsound

# if __name__ == "__main__" and 1 == 2:
# list_folder = file_helper.dqmllb_读取目录列表(pcml_排除目录=["util"])
# list_xlsx_file_name = file_helper.get_files_name_list_获取当前目录下的文件名列表(
#     "xlsx"
# )
# xz_选择 = my_tkinter.单选框("选择文件", list_xlsx_file_name).get()
# folder_path = file_helper.tqwjmqz_提取文件前缀(xz_选择)
# folder_path_img = f"{folder_path}/img"
# file_helper.cjwjj_创建文件夹(folder_path=folder_path)
# file_helper.cjwjj_创建文件夹(folder_path=folder_path_img)
# df = pd.read_excel(xz_选择)
# # df = pd.read_excel(list_xlsx_file_name[0])
# for col_name in df.columns:
#     if "价格" in col_name:
#         df = df.rename(columns={col_name: "价格"})
#         break
# df = df[
#     [
#         "ASIN",
#         "商品标题",
#         "商品主图",
#         "商品详情页链接",
#         "大类目",
#         "小类目",
#         "月销量",
#         "价格",
#         "评分数",
#         "评分",
#     ]
# ]
# df["待办"] = 0
# site = "亚马逊美国"
# list_ser = []
# xz_下载 = dp_helpe_V_file.xz_下载_class()
# tpss_1688 = tpss_1688图片搜索(page_xz_下载=xz_下载, tzdsl_特征点数量=8)
# for index, row in df.iterrows():
#     if row["代办"] == 1:
#         continue
#     pro_ama = Product(
#         price=row["价格"],
#         url=row["商品详情页链接"],
#         url_img=row["商品主图"],
#         title=row["商品标题"],
#         site=site,
#     )
#     ser_ama = pro_ama.get_series()
#     file_i = File(folder_path=folder_path_img, file_qz="原图")
#     xz_下载.xzwj_下载文件(pro_ama.url_img, file_i)
#     ser_1688 = tpss_1688.get_ser_产品汇总(file=file_i)
#     if ser_1688 is None:
#         continue
#     ser_out = pandas_util.get_series_横向合并([ser_ama, ser_1688])
#     ser_out["待办"] = 1
#     pandas_util.dfgx_df更新(index=index, df=df, ser=ser_out)
#     break
# df.to_excel(f"{folder_path}/out.xlsx", index=False)
# xz_下载.close()
# tpss_1688.close()


if __name__ == "__main__" and 1 == 2:
    set1 = {}
    list_xlsx_file_name = (
        file_helper_V_file.get_files_name_list_获取当前目录下的文件名列表("xlsx")
    )
    xz_选择 = my_tkinter.单选框("选择文件", list_xlsx_file_name).get()
    folder_path = file_helper_V_file.tqwjmqz_提取文件前缀(xz_选择)
    folder_path_img = f"{folder_path}/img"
    bl = file_helper_V_file.pdwjsfcz_判断文件是否存在(f"{folder_path}/out.xlsx")
    file_json = File(folder_path=folder_path, file_name="配置文件.json")
    if not bl:
        list_zd_站点 = ["亚马逊德国", "亚马逊日本", "亚马逊加拿大", "1688"]
        list_xz_选择 = my_tkinter.多选框("请选择你喜欢的选项：", list_zd_站点).get()
        set1["站点列表"] = list_xz_选择
        set1["图片识别特征点数"] = 30
        file_helper_V_file.cjwjj_创建文件夹(folder_path=folder_path)
        file_helper_V_file.cjwjj_创建文件夹(folder_path=folder_path_img)
        file_helper_V_file.xrjson_写入json文件(set1=set1, file=file_json)
        df = pd.read_excel(xz_选择)
    else:
        set1 = file_helper_V_file.dqjson_读取json文件(file=file_json)
        df = pd.read_excel(f"{folder_path}/out.xlsx")
    for col_name in df.columns:
        if "价格" in col_name:
            df = df.rename(columns={col_name: "价格"})
            break
    if "进度" not in df.columns:
        df["进度"] = ""
        df = df[
            [
                "ASIN",
                "商品标题",
                "商品主图",
                "商品详情页链接",
                "大类目",
                "小类目",
                "月销量",
                "价格",
                "评分数",
                "评分",
                "进度",
            ]
        ]
    else:
        df["进度"] = df["进度"].astype(str)
    tzds_特征点数 = set1["图片识别特征点数"]
    list_zd_站点 = set1["站点列表"]
    xz_下载 = dp_helpe_V_file.xz_下载_class()
    tpss_图片搜索 = None
    # for i in range(0, len(list_zd_站点)):
    # site = list_zd_站点[i]
    for site in list_zd_站点:
        # 判断执行什么查找策略
        if True:
            if site == "1688":
                tpss_图片搜索 = tpss_1688图片搜索(
                    page_xz_下载=xz_下载, tzdsl_特征点数量=tzds_特征点数
                )
            elif "亚马逊" in site:
                tpss_图片搜索 = cpss_ama_亚马逊产品搜索(
                    page_xz_下载=xz_下载, site=site, tzdsl_特征点数量=tzds_特征点数
                )
        for index, row in df.iterrows():
            if site in row["进度"]:
                continue
            pro_ama = Product(
                price=row["价格"],
                url=row["商品详情页链接"],
                url_img=row["商品主图"],
                title=row["商品标题"],
                site=site,
            )
            ser_ama = pro_ama.get_series()
            file_i = File(folder_path=folder_path_img, file_qz="原图")
            xz_下载.xzwj_下载文件(pro_ama.url_img, file_i)
            # 更新df
            if 1 == 1:
                row["进度"] += site + ","
                if file_i.get_file_name() is None:
                    ser_out = row
                else:
                    ser_hz = tpss_图片搜索.get_ser_产品汇总(file=file_i, prod=pro_ama)
                    if ser_hz is None:
                        ser_out = row
                    else:
                        ser_out = pandas_util.get_series_横向合并([row, ser_hz])
                # elif ser_hz is None:
                #     ser_out = row
                # else:
                #     ser_out = pandas_util.get_series_横向合并([row, ser_hz])
                pandas_util.dfgx_df更新(index=index, df=df, ser=ser_out)
                df.to_excel(f"{folder_path}/out.xlsx", index=False)
        tpss_图片搜索.close()
    df.to_excel(f"{folder_path}/out.xlsx", index=False)
    xz_下载.close()


# if __name__ == "__main__" and 1 == 1:
def fun1(xlsx: str, list_zd_站点list, tzds_特征点数):
    # set1 = {}
    folder_path = file_helper_V_file.tqwjmqz_提取文件前缀(xlsx)
    folder_path_img = f"{folder_path}/img"
    bl = file_helper_V_file.pdwjsfcz_判断文件是否存在(f"{folder_path}/out.xlsx")
    # file_json = File(folder_path=folder_path, file_name="配置文件.json")
    if not bl:
        # list_zd_站点 = ["1688", "亚马逊德国", "亚马逊日本", "亚马逊加拿大"]
        # set1["站点列表"] = list_zd_站点list
        # set1["图片识别特征点数"] = tzds_特征点数
        file_helper_V_file.cjwjj_创建文件夹(folder_path=folder_path)
        file_helper_V_file.cjwjj_创建文件夹(folder_path=folder_path_img)
        # file_helper_V_file.xrjson_写入json文件(set1=set1, file=file_json)
        df = pd.read_excel(xlsx)
    else:
        # set1 = file_helper_V_file.dqjson_读取json文件(file=file_json)
        df = pd.read_excel(f"{folder_path}/out.xlsx")
    for col_name in df.columns:
        if "价格" in col_name:
            df = df.rename(columns={col_name: "价格"})
            break
    if "进度" not in df.columns:
        df["进度"] = ""
        df = df[
            [
                "ASIN",
                "商品标题",
                "商品主图",
                "商品详情页链接",
                "大类目",
                "小类目",
                "月销量",
                "价格",
                "评分数",
                "评分",
                "进度",
            ]
        ]
    else:
        df["进度"] = df["进度"].astype(str)
    # tzds_特征点数 = set1["图片识别特征点数"]
    # list_zd_站点 = set1["站点列表"]
    xz_下载 = dp_helpe_V_file.xz_下载_class()
    tpss_图片搜索 = None
    # for i in range(0, len(list_zd_站点)):
    # site = list_zd_站点[i]
    for site in list_zd_站点list:
        # 判断执行什么查找策略
        if True:
            if site == "1688":
                tpss_图片搜索 = tpss_1688图片搜索(
                    page_xz_下载=xz_下载, tzdsl_特征点数量=tzds_特征点数
                )
            elif "亚马逊" in site:
                tpss_图片搜索 = cpss_ama_亚马逊产品搜索(
                    page_xz_下载=xz_下载, site=site, tzdsl_特征点数量=tzds_特征点数
                )
        for index, row in df.iterrows():
            try:
                if site in row["进度"]:
                    continue
                pro_ama = Product(
                    price=row["价格"],
                    url=row["商品详情页链接"],
                    url_img=row["商品主图"],
                    title=row["商品标题"],
                    site=site,
                )
                # ser_ama = pro_ama.get_series()
                file_i = File(folder_path=folder_path_img, file_qz="原图")
                xz_下载.xzwj_下载文件(pro_ama.url_img, file_i)
                # 更新df
                if 1 == 1:
                    row["进度"] += site + ","
                    if file_i.get_file_name() is None:
                        ser_out = row
                    else:
                        ser_hz = tpss_图片搜索.get_ser_产品汇总(
                            file=file_i, prod=pro_ama
                        )
                        if ser_hz is None:
                            ser_out = row
                        else:
                            ser_out = pandas_util.get_series_横向合并([row, ser_hz])
                    # elif ser_hz is None:
                    #     ser_out = row
                    # else:
                    #     ser_out = pandas_util.get_series_横向合并([row, ser_hz])
                    pandas_util.dfgx_df更新(index=index, df=df, ser=ser_out)
                    df.to_excel(f"{folder_path}/out.xlsx", index=False)
            except Exception as e:
                # a = 60
                # winsound.Beep(1000, int(1000 * a))
                print("main260 捕捉到错误")
                print(e)
                continue
        tpss_图片搜索.close()
    df.to_excel(f"{folder_path}/out.xlsx", index=False)
    xz_下载.close()


if __name__ == "__main__":
    list_xlsx_file_name = (
        file_helper_V_file.get_files_name_list_获取当前目录下的文件名列表("xlsx")
    )
    list_xlsx = my_tkinter.多选框("请选择你喜欢的选项：", list_xlsx_file_name).get()
    # xz_选择 = my_tkinter.单选框("选择文件", list_xlsx_file_name).get()
    # folder_path = file_helper_V_file.tqwjmqz_提取文件前缀(xz_选择)
    list_zd_站点 = ["亚马逊德国", "亚马逊日本", "亚马逊加拿大", "1688"]
    list_xz_选择 = my_tkinter.多选框("请选择你喜欢的选项：", list_zd_站点).get()
    for xlsx in list_xlsx:
        fun1(xlsx=xlsx, list_zd_站点list=list_xz_选择, tzds_特征点数=30)

if __name__ == "__main__" and 1 == 2:
    xz_下载 = dp_helpe_V_file.xz_下载_class()
    file = File(folder_path="img", file_name="1.jpg")
    prod = Product_Amazon(
        price="22,18 €",
        url="aa",
        url_img="",
        title="Ninja BC151BK Blast Portable Blender, Cordless, 18oz. Vessel, Personal Blender-for Shakes & Smoothies, BPA Free, Leakproof-Lid & Sip Spout, USB-C Rechargeable, Dishwasher Safe Parts, Black",
        site="亚马逊加拿大",
        score=1,
        number_comments=2,
    )
    cpss_产品搜索 = cpss_ama_亚马逊产品搜索(page_xz_下载=xz_下载, site="亚马逊加拿大")
    # cpss_产品搜索.prod = prod
    lis = cpss_产品搜索.get_ser_产品汇总(file=file, prod=prod)
    # list_prod = cpss_产品搜索.get_list_btss_标题搜索()
    # for prod in list_prod:
    #     print(prod.get_set())
    # tpss_图片搜索.test()
    # tpss_图片搜索.get_list_tpss_图片搜索(file=file)
    # a = tpss_图片搜索.get_list_img_获取相似图片列表(file=file)
    cpss_产品搜索.close()
    xz_下载.close()


if __name__ == "__main__" and 1 == 2:
    a = Product_Amazon(
        price="22,18 €",
        url="aa",
        url_img="",
        title="",
        site="",
        score=1,
        number_comments=2,
    )
    print(a.get_series())

if __name__ == "__main__" and 1 == 2:
    xz_下载 = dp_helpe_V_file.xz_下载_class()
    file = File(folder_path="img", file_name="1.jpg")
    xz_下载.xzwj_下载文件(
        url="https://cbu01.alicdn.com/https://cbu01.alicdn.com/img/ibank/O1CN01r34wGk1MZgoBz4IO2_!!2216109401449-0-cib.jpg_460x460q100.jpg",
        file=file,
    )

# if __name__ == "__main__" and 1 == 2:
#     xz_下载 = dp_helpe_V_file.xz_下载_class()
#     cpss_产品搜索 = cpss_ama_亚马逊产品搜索(page_xz_下载=xz_下载, site="亚马逊德国")
#     file = File(folder_path="img", file_name="1688.jpg")
#     cpss_产品搜索.tpss_图片搜索(file=file)
#     xz_下载.close()
#     cpss_产品搜索.close()

# if __name__ == "__main__" and 1 == 2:
#     set1 = {"a": 1}
#     file = File(folder_path=".", file_name="a.json")
#     file_helper_V_file.xrjson_写入json文件(set1=set1, file=file)


# if __name__ == "__main__" and 1 == 2:
#     a = Product(
#         price=1, url="www.1688.com", url_img="asfdjl", title="title", site=str(1688)
#     ).get_series()
#     b = Product(
#         price=2, url="www.1688.com", url_img="asfdjl", title="title", site=str(11)
#     ).get_series()
#     ser_out = pandas_util.get_series_横向合并([a, b])
#     # df = pd.DataFrame(a,name=a.index).T
#     df = pd.DataFrame([a])
#     # df = pd.concat([a], axis=0).T
#     # df.iloc[0] = ser_out
#     pandas_util.dfgx_df更新(0, df=df, ser=ser_out)
#     df.to_excel("out.xlsx", index=True)


# if __name__ == "__main__" and 1 == 2:
#     xz_下载 = dp_helper.xz_下载_class()
#     pc1688 = pc_爬虫_service.pc1688_class(xz_下载)
#     # pc1688.sstp_搜索图片("1/1.jpg", tzdsl_特征点数量=30)
#     pc1688.sstp_搜索图片("1/3.png", tzdsl_特征点数量=30)
#     # img1 = "1/3.png"
#     # img2 = "1/2.png"
#     # cv2_util.pdtpsfxs_判断图片是否相似(
#     #     img1_path=img1, img2_path=img2, tzdsl_特征点数量=30, sfksh_是否可视化=True
#     # )

# if __name__ == "__main__" and 1 == 2:
#     xz_下载 = dp_helper.xz_下载_class()
#     xz_下载.xzwj_下载文件(
#         url="https://cbu01.alicdn.com/img/ibank/O1CN01U7PBXL1ailKGXPjrL_!!2216502853364-0-cib.jpg",
#         folder_path="img",
#         file_name="1",
#     )

# if __name__ == "__main__" and 1 == 2:
#     xz_下载 = dp_helpe_V_file.xz_下载_class()
#     file = File(folder_path="1", file_name="1.png")
#     tpss_1688 = tpss_1688图片搜索(page_xz_下载=xz_下载, tzdsl_特征点数量=8)
#     # tpss_1688.get_list_product_获取产品列表(file=file)
#     # tpss_1688.get_list_img_获取相似图片列表(file=file)
#     # df = tpss_1688.get_df_产品汇总(file=file)
#     ser = tpss_1688.get_ser_产品汇总(file=file)
#     pd.DataFrame(ser).to_excel("1.xlsx", index=False)
#     tpss_1688.close()
#     xz_下载.close()

# # if __name__ == "__main__":
# #     list_xlsx_file_name = file_helper.get_files_name_list_获取当前目录下的文件名列表(
# #         "xlsx"
# #     )
# #     xz_选择 = my_tkinter.单选框("选择文件", list_xlsx_file_name).get()
# #     df = pd.read_excel(xz_选择)
# #     for i_ser in df.iterrows():
# #         pro_ama=Product(price=)
# # df = pd.read_excel(list_xlsx_file_name[0])
